<template>
  <div class="middleground" v-if="isFull == 0">
    <headers @changeNav="changeNav"></headers>
    <div class="main">
      <div class="l-main">
        <background-map
          :selectedNotice="selectedNotice"
          :showAlarmRecords="showAlarmRecords"
        ></background-map>
        <noticeList @changeAlarmDialog="changeAlarmDialog"></noticeList>
        <alarm-records
          v-if="showAlarmRecords"
          @changeAlarmDialog="changeAlarmDialog"
        ></alarm-records>
      </div>
      <div class="r-main">
        <router-view
          :showAlarmRecords="showAlarmRecords"
          :selectedNotice="selectedNotice"
          :alarm="alarm"
          @changeAlarm="changeAlarm"
          @changeAlarmDialog="changeAlarmDialog"
        />
      </div>
    </div>
  </div>
  <div class="app" v-if="isFull == 1">
    <router-view />
  </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
import API from 'api'
import headers from '@/components/layout/header.vue'
import backgroundMap from '@/components/Map/map.vue'
import noticeList from '@/components/Notice/notice.vue'
import statistics from '@/components/Event/statistics.vue'
import alarmRecords from '@/components/Notice/alarmRecords.vue'
import { useMqtt } from '@/hooks/useMqtt.js'
import { useEventbus } from "@/hooks/useEventBus.js";
import dayjs from 'dayjs'

export default {
  components: {
    headers,
    backgroundMap,
    noticeList,
    statistics,
    alarmRecords,
  },
  setup() {
    let state = reactive({
      showModule: 'statistics',
      showAlarmRecords: false,
      selectedNotice: {},
      isFull: 1,
      alarm: false
    })
    const eventBus = useEventbus()
    API.menu().then((res) => {
      let ids = res.data
      window.menu = ids
    })
    let path = window.location.pathname
    if (path != '/produce' && path != '/produceLogin' && path != '/login' && path != '/alexaLogin' && path != '/privacy-policy') {
      // 不是登录页才鉴权
      API.userInfo().then((res) => {
        window.userInfo = res.data
        if (res.code != 0) {
          state.isFull = 1
          window.localStorage.removeItem('auth_token')
          window.localStorage.removeItem('user_id')
          window.localStorage.removeItem('username')
          window.location.href = '/login'
        } else if (res.code == 0 && path == '/') {
          // 大屏
          state.isFull = 1
          window.localStorage.setItem('username', res.data.real_name)
        } else {
          state.isFull = 0
          window.localStorage.setItem('username', res.data.real_name)
        }
      })
    }

    // 导航切换
    const changeNav = (module) => {
      state.showModule = module
    }

    // 展示报警列表
    const changeAlarmDialog = (item, val) => {
      state.selectedNotice = item
      state.showAlarmRecords = val ?? true
    }

    const receiveMessage = (msg) => {
      console.log(msg)
    }

    const changeAlarm = () => {
      state.alarm = false // 置空状态，方便下次数据报表数据叠加
    }

    // mqtt 初始化
    const { init } = useMqtt()
    init(['/1/#', '/2/#'], (topic, msg) => {
      let data = JSON.parse(msg.toString())
      let arr = topic.split('/');
      // 报警消息
      if(arr[1] == 1 && arr[3] == 'alarm' ) {
        API.getDeviceInfoBySN({sn: arr[2], sensor_id: data.sensor_id}).then(res => {
          let title = 'Emergency alarming'
							switch (res.data.child_device.device_type) {
								case "PIR", "WOS", "DC":
									title = "Intrusion alarming"
									break;
								case "WSD":
									title = "Smoke fire alarming"
									break;
								case "GLS":
									title = "Gas leakage alarming"
									break;
								case "WLS":
									title = "Water leakage alarming"
									break;
							}
          const now = dayjs().format('YYYY-MM-DD HH:mm:ss')
          let msgData = {
            sn: arr[2].toString(),
            sensor_id: data.sensor_id,
            time: now,
            created_at: now,
            info: res.data.device,
            type: data.type,
            alarm_status: data.type,
            resource:  `[${title}] ${res.data.child_device.area_name}: ${title} on ${data.name}!`
          }
          state.alarm = true
          console.log('state===', state.alarm)
          eventBus.emitterEmit('AlarmMessage', msgData)
        })
      }
    })

    return {
      changeNav,
      changeAlarm,
      changeAlarmDialog,
      receiveMessage,
      ...toRefs(state),
    }
  },
}
</script>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

// 谷歌地图
.gm-style-iw { height: 280px; max-height:280px !important;width: 500px !important; max-width: 500px !important;}
.gm-style-iw-d {height: 280px; max-height: 280px !important; font-size: 16px;}
.main {
  box-sizing: border-box;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 6.25rem;
  overflow: hidden;

  .l-main {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex-wrap: wrap;
    box-sizing: border-box;
    width: 35vw;
    padding-right: 40px;
  }

  .r-main {
    position: relative;
    width: 60vw;
    height: 82vh;
    border-radius: 5px;
    box-shadow: 1px 2px 7px #3b3b3b;
    margin: 20px 40px;
    overflow: hidden;
  }
}
.flex_start {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.flex_between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex_center {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
/* 滚动槽 */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    border-radius: 3px;
    background: rgba(0,0,0,0.06);
    box-shadow: inset 0 0 5px rgba(0,0,0,0.08);
}
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background: rgba(0,0,0,0.12);
    box-shadow: inset 0 0 10px rgba(0,0,0,0.2);
}

</style>
